var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
const pay = require('../../../services/pay.js');

Page({
  data: {
    orderId: 0,
    orderInfo: {},
    orderGoods: [],
    handleOption: {},
    shippingList:[],
  },
  //页面初始化 options为页面跳转所带来的参数 
  onLoad: function (options) {
    this.setData({
      orderId: options.id
    });
    this.getOrderDetail();
  },
  //请求订单详情
  getOrderDetail() {
    let that = this;
    util.request(api.OrderDetail, {
      orderId: that.data.orderId
    }).then(function (res) {
      if (res.errno === 0) {
        that.setData({
          orderInfo: res.data.orderInfo,
          orderGoods: res.data.orderGoods,
          handleOption: res.data.handleOption,
        });
        if (res.data.shippingList){
          that.setData({
            shippingList: res.data.shippingList,
          });
        }
        //that.payTimer();
      }
    });
  },
  //暂时没用
  payTimer() {
    let that = this;
    let orderInfo = that.data.orderInfo;
    setInterval(() => {
      console.log(orderInfo);
      orderInfo.add_time -= 1;
      that.setData({
        orderInfo: orderInfo,
      });
    }, 1000);
  },
  //取消订单
  cancelOrder(){
    let that = this;
    let orderInfo = that.data.orderInfo;

    var order_status = orderInfo.order_status;
    console.log(order_status);

    var errorMessage = '';
    switch (order_status){
      case 101: {
        console.log('已经取消');
        errorMessage = '订单已取消';
        break;
      }
      case 102: {
        console.log('已经删除');
        errorMessage = '订单已删除';
        break;
      }
      case 300: {
        console.log('已发货，不能取消');
        errorMessage = '订单已发货';
        break;
      }
      case 301:{
        console.log('已收货，不能取消');
        errorMessage = '订单已收货';
        break;
      }
      case 401: {
        console.log('已经退款');
        errorMessage = '订单已退款';
        break;
      }
      case 402: {
        console.log('已经退款退货');
        errorMessage = '订单已退货';
        break;
      }
    }
      
    if (errorMessage != '') {
      console.log(errorMessage);
      util.showErrorToast(errorMessage);
      return false;
    }
    
    wx.showModal({
      title: '',
      content: '确定要取消此订单？',
      success: function (res) {
        if (res.confirm) {
          util.request(api.OrderCancel,{orderId: orderInfo.id}).then(function (res) {
            if (res.errno === 0) {
              wx.showModal({
                title:'提示',
                content: res.errmsg,
                showCancel:false,
                confirmText:'继续',
                success: function (res) {
                  //util.redirect('/pages/ucenter/order/order');
                  wx.navigateBack({
                    url: 'pages/ucenter/order/order',
                  });
                }
              });
            } else {
              util.showErrorToast(res.errmsg);
            }
          });
        }
      }
    });
  },
  //去付款
  payOrder() {
    let that = this;
    const orderId = that.data.orderId;
    pay.payOrder(parseInt(orderId)).then(res => {
      wx.redirectTo({
        url: '/pages/payResult/payResult?status=1&orderId=' + orderId
      });
    }).catch(res => {
      wx.redirectTo({
        url: '/pages/payResult/payResult?status=0&orderId=' + orderId
      });
    });

    /*
    util.request(api.PayPrepayId, {
      orderId: that.data.orderId
    }).then(function (res) {
      console.log("支付请求参数", res);
      if (res.errno === 0) {
        const payParam = res.data;
        wx.requestPayment({
          'timeStamp': payParam.timeStamp,
          'nonceStr': payParam.nonceStr,
          'package': payParam.package,
          'signType': payParam.signType,
          'paySign': payParam.paySign,
          'success': function (res) {
            console.log(res);
          },
          'fail': function (res) {
            console.log(res);
          }
        });
      } else {
        util.showErrorToast(res.errmsg);
      }
    });
    */
  },
  //删除订单
  deleteOrder(){
    let that = this;
    let orderInfo = that.data.orderInfo;

    if (!that.data.handleOption.delete){
      util.showErrorToast("不可被删除");
      return;
    }
    
    wx.showModal({
      title: '',
      content: '确定要删除此订单？',
      success: function (res) {
        if (res.confirm) {
          util.request(api.OrderDelete, { orderId: orderInfo.id }).then(function (res) {
            if (res.errno === 0) {
              wx.showModal({
                title: '提示',
                content: res.errmsg,
                showCancel: false,
                confirmText: '继续',
                success: function (res) {
                  wx.navigateBack({
                    url: 'pages/ucenter/order/order',
                  });
                }
              });
            } else {
              util.showErrorToast(res.errmsg);
            }
          });
        }
      }
    });
  },
  //确认收货
  deliveryOrder() {
    let that = this;
    let orderInfo = that.data.orderInfo;

    if (!that.data.handleOption.delivery) {
      util.showErrorToast("不可进行此操作");
      return;
    }

    wx.showModal({
      title: '',
      content: '确定收货？',
      success: function (res) {
        if (res.confirm) {
          util.request(api.OrderDelivery, { orderId: orderInfo.id }).then(function (res) {
            if (res.errno === 0) {
              wx.showModal({
                title: '提示',
                content: res.errmsg,
                showCancel: false,
                confirmText: '继续',
                success: function (res) {
                  wx.navigateBack({
                    url: 'pages/ucenter/order/order',
                  });
                }
              });
            } else {
              util.showErrorToast(res.errmsg);
            }
          });
        }
      }
    });

  },
  //去评论
  toComment(e){
    var goods = JSON.stringify(e.currentTarget.dataset.item);
    wx.navigateTo({
      url: '/pages/commentPost/commentPost?goods=' + goods,
    })
  },
  //去查看物流
  lookTraces(e){
    var shippingList = JSON.stringify(this.data.shippingList);
    wx.navigateTo({
      url: '/pages/ucenter/traces/traces?shippingList=' + shippingList,
    })
  }
})